﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _2017_FormAjax : System.Web.UI.Page
{
    string returnMsg = "";
    private System.Diagnostics.PerformanceCounter performanceCounter1;
    DateTime endtime = new DateTime(2020, 5, 30, 23, 59, 59);
    string tablename = "Event_2020_rc0428";
    public static object thislock = new object();
    string openid = "测试openid";
    string nickname = "测试nickname";
    string headimg = "测试headimg";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["type"]))
        {
            Response.Write("非法请求");
            return;
        }
        else
        {
            WeiXinData wxData = CheckWXData();
            if (wxData == null)
            {
                Response.Write("{\"state\":-1,\"message\":\"授权失败，未获取用户微信基本信息\"}");
                return;
            }
            else
            {
                openid = wxData.openid;
                nickname = wxData.nickName;
                headimg = wxData.imgUrl;
            }
            string functionName = Request.Form["Type"].Trim().ToLower();
            switch (functionName)
            {
                case "formsubimit":
                    returnMsg = FormSubimit();
                    break;
                case "saveweixin":
                    returnMsg = SaveWechatInfo();
                    break;
                case "question":
                    returnMsg = Question();
                    break;
            };
            Response.Write(returnMsg);
            return;
        }
    }
    #region 以下为固定代码
    WeiXinData CheckWXData()
    {
        WeiXinData wxData = GetPingTaiWeiXinDataJson();
        return wxData;

    }
    public WeiXinData GetPingTaiWeiXinDataJson()
    {
        if (Session["PingTaiWeiXinData"] == null)
        {
            return null;
        }
        else
        {
            return (WeiXinData)Session["PingTaiWeiXinData"];
        }
    }
    #endregion

    //用户提交表单
    private string Question()
    {
        string i = "";
        //DateTime endtime = Convert.ToDateTime(SiteFun.cutechar(Request.Form["endTime"]));
        if (DateTime.Now > endtime)
        {
            i = "{\"state\":8,\"message\":\"本次活动已过期\"}";
            return i;
        }
        int sql;
        string ip = Request.ServerVariables.Get("Remote_Addr").ToString();
        string sex = SiteFun.cutechar(Request.Form["sex"]);
        string age = SiteFun.cutechar(Request.Form["age"]);
        string hangye = SiteFun.cutechar(Request.Form["hangye"]);
        string xiuxian = SiteFun.cutechar(Request.Form["xiuxian"]);
        string hobby = SiteFun.cutechar(Request.Form["hobby"]);
        string drink = SiteFun.cutechar(Request.Form["drink"]);
        string weidao = SiteFun.cutechar(Request.Form["weidao"]);
        string caixi = SiteFun.cutechar(Request.Form["caixi"]);
        string colour = SiteFun.cutechar(Request.Form["colour"]);
        string day = SiteFun.cutechar(Request.Form["day"]);
        string works = SiteFun.cutechar(Request.Form["works"]);
        int count = SiteFun.objectToInt(DbHelperSQL.GetSingle("select count(sex) from Event_2020_rc0428 where openid='" + openid + "'"), 0);
        if (count > 0)
        {
            i = "{\"state\":2,\"message\":\"不能重复提交问卷\"}";
            return i;
        }
            SqlParameter[] parameters = {
            new SqlParameter("@openid", SqlDbType.NVarChar, 50),
            new SqlParameter("@sex", SqlDbType.NVarChar, 100),
            new SqlParameter("@age", SqlDbType.NVarChar, 100),
            new SqlParameter("@hangye", SqlDbType.NVarChar, 100),
            new SqlParameter("@xiuxian", SqlDbType.NVarChar, 100),
            new SqlParameter("@hobby", SqlDbType.NVarChar, 100),
            new SqlParameter("@drink", SqlDbType.NVarChar, 100),
            new SqlParameter("@weidao", SqlDbType.NVarChar, 100),
            new SqlParameter("@caixi", SqlDbType.NVarChar, 100),
            new SqlParameter("@colour", SqlDbType.NVarChar, 100),
            new SqlParameter("@day", SqlDbType.NVarChar, 100),
            new SqlParameter("@works", SqlDbType.NVarChar, 100)};
            parameters[0].Value = openid;
            parameters[1].Value = sex;
            parameters[2].Value = age;
            parameters[3].Value = hangye;
            parameters[4].Value = xiuxian;
            parameters[5].Value = hobby;
            parameters[6].Value = drink;
            parameters[7].Value = weidao;
            parameters[8].Value = caixi;
            parameters[9].Value = colour;
            parameters[10].Value = day;
            parameters[11].Value = works;
            int res = DbHelperSQL.ExecuteSql("update Event_2020_rc0428 set sex=@sex,age=@age,hangye=@hangye,xiuxian=@xiuxian,hobby=@hobby,drink=@drink,weidao=@weidao,caixi=@caixi,colour=@colour,day=@day,works=@works where openid=@openid", parameters);
            if (res > 0)
            {
                //DataTable dt = DbHelperSQL.Query("select * from " + tablename + " where OpenID = '" + openid + "'").Tables[0];
                i = "{\"state\":1,\"message\":\"已成功提交问卷\"}";
            }
            else
            {
                i = "{\"state\":0,\"message\":\"报名失败请刷新后再试\"}";
            }

        return i;
    }

    //用户提交表单
    private string FormSubimit()
    {
        string i = "";
        //DateTime endtime = Convert.ToDateTime(SiteFun.cutechar(Request.Form["endTime"]));
        if (DateTime.Now > endtime)
        {
            i = "{\"state\":8,\"message\":\"本次活动已过期\"}";
            return i;
        }
        int sql;
        string ip = Request.ServerVariables.Get("Remote_Addr").ToString();
        string name = SiteFun.cutechar(Request.Form["name"]);
        string telephone = SiteFun.cutechar(Request.Form["tel"]);
        string agree = SiteFun.cutechar(Request.Form["agree"]);
        sql = SiteFun.objectToInt(DbHelperSQL.GetSingle("select COUNT(1) from (select * from Event_2020_rc0428 where OpenId!='" + openid + "') a where telephone='" + telephone + "'"), 0);
        int count = SiteFun.objectToInt(DbHelperSQL.GetSingle("select count(name) from Event_2020_rc0428 where openid='" + openid + "'"), 0);
        if (count > 0)
        {
            i = "{\"state\":3,\"message\":\"不能重复报名\"}";
            return i;
        }
        //判断手机是否被使用过
        if (sql > 0)
        {
            i = "{\"state\":2,\"message\":\"手机已被报名\"}";
        }
        else
        {
            SqlParameter[] parameters = {
            new SqlParameter("@openid", SqlDbType.NVarChar, 50),
            new SqlParameter("@name", SqlDbType.NVarChar, 50),
            new SqlParameter("@tel", SqlDbType.NVarChar, 50),
            new SqlParameter("@agree", SqlDbType.NVarChar, 50)};
            parameters[0].Value = openid;
            parameters[1].Value = name;
            parameters[2].Value = telephone;
            parameters[3].Value = agree;
            int res = DbHelperSQL.ExecuteSql("update Event_2020_rc0428 set name=@name,telephone=@tel,agree=@agree where openid=@openid", parameters);
            if (res > 0)
            {
                //DataTable dt = DbHelperSQL.Query("select * from " + tablename + " where OpenID = '" + openid + "'").Tables[0];
                i = "{\"state\":1,\"message\":\"已成功报名\"}";
            }
            else
            {
                i = "{\"state\":0,\"message\":\"报名失败请刷新后再试\"}";
            }

        }

        return i;
    }
    //微信用户授权验证
    private string SaveWechatInfo()
    {
        string i = "";
        if (DateTime.Now > endtime)
        {
            i = "{\"state\":8,\"message\":\"本次活动已过期\"}";
            return i;
        }
        else
        {
            i = SaveWechatInfo(openid, nickname, headimg);

        }
        return i;
    }

    //验证用户是否报名方法
    public string SaveWechatInfo(string openid, string nickname, string headimg)
    {
        int sql = SiteFun.objectToInt(DbHelperSQL.GetSingle("SELECT COUNT(1) FROM " + tablename + " WHERE  OpenId ='" + openid + "'"), 0);
        if (sql > 0)
        {
            string sex = SiteFun.objectToStr(DbHelperSQL.GetSingle("SELECT sex FROM " + tablename + " WHERE OpenId='" + openid + "'"));
            if (sex == "" || sex == null)
            {
                return "{\"state\":2,\"message\":\"未提交调查问卷\"}";
            }
            else
            {
                DataTable dt = DbHelperSQL.Query("select * from " + tablename + " where OpenId = '" + openid + "'").Tables[0];
                return "{\"state\":3,\"message\":\"已提交调查问卷，并且返回用户信息\",\"rows\":" + SiteFun.DataTable3Json(dt, "str") + "}";
            }
        }
        else
        {
            DbHelperSQL.ExecuteSql("INSERT INTO " + tablename + "([OpenId],[NickName],[HeadImg],[AddTime]) VALUES ('" + openid + "','" + nickname + "','" + headimg + "',GETDATE())");
            return "{\"state\":1,\"message\":\"添加微信信息完成\"}";
        }
    }

}